<?php

namespace App\Http\Controllers\Frontend\V10\Message;

use App\Http\Requests\Frontend\V10\SysMessageIndexRequest;
use App\Models\MessageSended;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

class SysMessageController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth:api');
    }

    /**
     * Description:用户消息列表
     * Author: Sam
     * DateTime: 2019/1/12 11:45
     * @param SysMessageIndexRequest $request
     * @return mixed
     */
    public function index(SysMessageIndexRequest $request)
    {
        $user = Auth::user();
        $page_size = $request->get('page_size',null);
        if($page_size){
            $list = MessageSended::where('user_id',$user->id)
                ->orderBy('status')
                ->orderByDesc('created_at')
                ->paginate($page_size);
        }else{
            $list = MessageSended::where('user_id',$user->id)
                ->orderBy('status')
                ->orderByDesc('created_at')
                ->get();
        }
        return success($list);
    }

    /**
     * Description:读消息
     * Author: Sam
     * DateTime: 2019/1/12 11:46
     * @param MessageSended $msg
     * @return mixed
     */
    public function read(MessageSended $msg)
    {
        $user = Auth::user();
        if($user->id!==$msg->user_id){
            return failed('参数错误');
        }
        $msg->status = 1;
        $msg->save();
        return success([]);
    }
}
